#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.basic.module.operate.service.impl;

import ${package}.basic.module.operate.mapper.OperateMapper;
import ${package}.basic.module.operate.service.OperateService;
import ${package}.basic.module.permission.mapper.PermissionMapper;
import ${package}.common.bean.po.Operate;
import ${package}.common.bean.po.OperatePermission;
import ${package}.common.bean.vo.Constraint;
import ${package}.common.bean.vo.QueryBean;
import ${package}.common.emum.OperateStatusEnum;
import ${package}.common.emum.ReturnCodeEnum;
import ${package}.common.exception.ValidException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 无法补充
 *
 * @author zhang wen yi
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Service
@AllArgsConstructor
public class OperateServiceImpl implements OperateService {
    /**
     * 操作Mapper
     */
    private OperateMapper operateMapper;

    /**
     * 权限Mapper
     */
    private PermissionMapper permissionMapper;


    /**
     * Description:
     * <获取操作管理分页列表>
     * @author pig
     * @date 9:30 2019/8/7
     * @param queryBean 1
     * @return com.github.pagehelper.PageInfo<Operate>
     **/
    @Override
    public PageInfo<Operate> queryOperteByLimit(QueryBean queryBean){

        // 筛选状态
        Constraint constraint = Constraint.build()
                .putField("o.status")
                .putOp(Constraint.LOPERATE.IN.getOperation())
                .putValus(Boolean.TRUE, OperateStatusEnum.ENABLED.getCode(), OperateStatusEnum.DISABLE.getCode())
                .putValueType(Constraint.DATATYPE.STRING.getType());
        queryBean.and(constraint);

        Page<Operate> page = PageMethod.startPage(queryBean.getCurrent(), queryBean.getPageSize());
        operateMapper.queryOperteByLimit(queryBean);

        return new PageInfo<>(page);
    }

    /**
     * Description:
     * <根据主键查询操作信息>
     * @author pig
     * @date 9:30 2019/8/7
     * @param id 1
     * @return Operate
     **/
    @Override
    public Operate queryOperareByKey(Long id){
        return operateMapper.queryOperateByOperateKey(id);
    }

    /**
     * Description:
     * <新增操作管理信息>
     * @author pig
     * @date 9:30 2019/8/7
     * @param operate 1
     **/
    @Override
    public void addOperate(Operate operate){
        operateMapper.addOperate(operate);
    }

    /**
     * Description:
     * <根据主键更新操作管理信息>
     * @author pig
     * @date 9:30 2019/8/7
     * @param operate 1
     **/
    @Override
    public void updateOperateByKey(Operate operate){
        operateMapper.updateOperateByKey(operate);
    }

    /**
     * Description:
     * <根据主键删除操作管理信息>
     * @author pig
     * @date 9:33 2019/8/7
     * @param id 1
     **/
    @Override
    public void deleteOperateByKey(Long id){
        // 查询操作管理是否有角色占用
        int i = permissionMapper.queryOperateOperatePermissionCountByKey(id);
        if(i != 0){
            throw new ValidException(ReturnCodeEnum.CURRENT_ELEMENT_WAS_OCCUPIED);
        }

        operateMapper.deleteOperateByKey(id);
    }

    /**
     * Description:
     * <批量删除操作管理>
     * @author pig
     * @date 9:33 2019/8/7
     * @param keys 1
     * @return java.util.List<Operate>
     **/
    @Override
    public List<Operate> removeOperatesBykeys(List<Long> keys){

        // 区分被角色占用的操作管理
        List<Long> occupied = new ArrayList<>();
        List<Long> free = new ArrayList<>();

        // 判断是否有角色占用操作管理权限
        List<OperatePermission> operatePermissions = permissionMapper.findOperatesByOperateKeys(keys);
        for (Long key: keys) {
            boolean flag = false;
            for (OperatePermission operatePermission: operatePermissions) {
                if (key.equals(operatePermission.getOperateId())){
                    flag = true;
                    break;
                }
            }
            if (flag){
                occupied.add(key);
            }else {
                free.add(key);
            }
        }

        // 根据主键批量将操作管理置为删除
        if (!free.isEmpty()){
            operateMapper.updateOperateStatusDeleteByKeys(free);
        }

        if (!occupied.isEmpty()){
            return operateMapper.findOperatesByKeys(occupied);
        }else {
            return Lists.newArrayList();
        }
    }

    /**
     * Description:
     * <禁用/启用>
     * @author pig
     * @date 9:33 2019/8/7
     * @param operate 1
     **/
    @Override
    public void disableOrEnableOperate(Operate operate){

        // 查询操作管理是否有角色占用
        int i = permissionMapper.queryOperateOperatePermissionCountByKey(operate.getId());
        if(i != 0){
            throw new ValidException(ReturnCodeEnum.CURRENT_ELEMENT_WAS_OCCUPIED);
        }
        //判断传来的状态更新为相反状态
        if(operate.getStatus().intValue() == OperateStatusEnum.ENABLED.getCode().intValue()){
            operate.setStatus(OperateStatusEnum.DISABLE.getCode());
        }else {
            operate.setStatus(OperateStatusEnum.ENABLED.getCode());
        }

        operateMapper.updateOperateDisableOrEnable(operate);
    }

    /**
     * Description:
     * <查询编码是否重复>
     * @author pig
     * @date 9:35 2019/8/7
     * @param code 1
     * @param id 2
     * @return java.lang.Boolean
     **/
    @Override
    public Boolean operateVerifyCode(String code, Long id){

        if(id == null){
	        return CollectionUtils.isEmpty(operateMapper.findOperatesByCode(code));
        } else {
	        return CollectionUtils.isEmpty(operateMapper.findOperatesByCodeOrNotInId(new Operate(id, code)));
        }
    }
}
